#!/bin/bash

passwd=$1
kubeVersion=$2
vMasterCount=$3
vWorkerCount=$4
ipNetwork=$5
ipStart=$6
dns1=$7
dns2=$8

echo -e "\n\n=========== 安装必要软件 ================"
yum install -y nfs-utils

echo -e "\n\n=========== 修改静态ip ================"
sudo ls /etc/sysconfig/network-scripts/
enp=$(ls /etc/sysconfig/network-scripts/|egrep ifcfg |grep eth1.*|awk -F"-" '{print $2}')
enpway=/etc/sysconfig/network-scripts/ifcfg-$enp
rt=$(netstat -rn|head -3|tail -1|awk -F" " '{print $2}')
sed -i '/BOOTPROTO/c BOOTPROTO=static' $enpway
sed -i '/ONBOOT/c ONBOOT=yes' $enpway
# sed -i '/NM_CONTROLLED/c NM_CONTROLLED=no' $enpway
cat >>/etc/sysconfig/network-scripts/ifcfg-$enp <<EOF
DNS1=$dns1
DNS2=$dns2
EOF
sudo systemctl restart NetworkManager


echo -e "\n\n=========== Update /etc/hosts=========== "
for ((i=1; i<=$vMasterCount; i++)); do
    echo "${ipNetwork}.$((ipStart-1+i))  kmaster$i  kmaster$i.example.com" >>/etc/hosts
done

for ((i=1; i<=$vWorkerCount; i++)); do
    echo "${ipNetwork}.$((ipStart+vMasterCount-1+i))  kworker$i  kworker$i.example.com" >>/etc/hosts
done
echo "/etc/hosts >>>>"
cat /etc/hosts


echo -e "\n\n=========== Modify docker cgroupdriver=========== "
# 清空文件
:> /etc/docker/daemon.json
cat <<EOF | sudo tee /etc/docker/daemon.json
{
  "registry-mirrors": [
    "https://ustc-edu-cn.mirror.aliyuncs.com/",
    "https://52emalvj.mirror.aliyuncs.com",
    "https://docker.mirrors.ustc.edu.cn"
   ],
  "exec-opts":["native.cgroupdriver=systemd"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker


echo -e "\n\n=========== Add YUM Repo File For Kubernetes=========== "
cat >>/etc/yum.repos.d/kubernetes.repo<<EOF
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF


echo -e "\n\n=========== Install kubeadm, kubelet and kubectl=========== "
yum install -y -q kubeadm-$kubeVersion kubelet-$kubeVersion kubectl-$kubeVersion


echo -e "\n\n=========== Enable and Start Kubelet Service=========== "
systemctl enable kubelet
systemctl start kubelet


echo -e "\n\n=========== Setting Root Password=========== "
echo $passwd | passwd --stdin root
echo "export TERM=xterm" >> /etc/bashrc


echo -e "\n\n=========== Install helm=========== "
cp /vagrant/helm/helm /usr/bin
chmod +x /usr/bin/helm
